package com.lenovo.smarttraffic.ui.activity.stage2;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.CarInfo;
import com.lenovo.smarttraffic.bean.ChargeHistory;
import com.lenovo.smarttraffic.bean.UserBean;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.ui.adapter.ETCChargeHistoryAdapter;
import com.lenovo.smarttraffic.util.HttpUtils;

import java.util.List;
import java.util.Locale;

public class ETCChargeHistoryActivity extends BaseActivity {

    private ListView mListView;
    private ETCChargeHistoryAdapter adapter;

    private TextView charge_total;

    @Override
    protected int getLayout() {
        return R.layout.activity_etc_charge_history;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
        initData();
    }

    public void initView() {
        Toolbar mToolbar = findViewById(R.id.toolbar);
        mToolbar.setNavigationOnClickListener(view -> finish());

        mListView = findViewById(R.id.listview);

        charge_total = findViewById(R.id.charge_total);
    }

    public void initData() {
        HttpUtils.post(this, "get_car_account_record",
                new HttpUtils().setParam("UserName", MainActivity.userName),
                response -> HttpUtils.post(this, "get_all_user_info",
                        new HttpUtils().setParam("UserName", MainActivity.userName),
                        response2 -> HttpUtils.post(this, "get_car_info",
                                new HttpUtils().setParam("UserName", MainActivity.userName),
                                response3 -> {
                                    ChargeHistory chargeHistory = new Gson().fromJson(response.toString(), ChargeHistory.class);
                                    List<ChargeHistory.ROWSDETAILBean> list = chargeHistory.getROWS_DETAIL();
                                    int money = 0;
                                    for (int i = 0; i < list.size(); i++) {
                                        money += list.get(i).getMoney();
                                    }
                                    charge_total.setText(String.format(Locale.ENGLISH, "充值合计：%d元", money));

                                    adapter = new ETCChargeHistoryAdapter(this, chargeHistory,
                                            new Gson().fromJson(response2.toString(), UserBean.class),
                                            new Gson().fromJson(response3.toString(), CarInfo.class));
                                    mListView.setAdapter(adapter);
                                })
                ));
    }


}
